/**
 * \file TPType.cpp
 * \author Ameen Jaber
 * \author Mohamad Noureddine
 * \author Mohamed Sabra
 * \brief Contains implementation of TripleP types classes
 */
#include <TPType.h>

// default constructor
TPType::TPType() {
  // Initialize everything to be null
  id           = -1; 
  _t1          = NULL;
  _t2          = NULL;
}

// constructor
TPType::TPType(int _id, std::string _name, TP_Op_t _type) {
  // Initialize value appropriately
  id        = _id;
  type_name = _name;
  type      = _type;
  _t1       = NULL;
  _t2       = NULL;
}

// destructor
TPType::~TPType() {
  // do not change the pointer: the one who created them should free them 
  id = -1; //reset id to -1
}

// insert subtype
int TPType::insert_subtype(TPType * ptr) {
  if (_t1 == NULL) {// t1 has not been assigned
    _t1 = ptr; // check here if i should allocate and copy or simply assign    
    return 1;
  } else if (_t2 == NULL) {// t1 assigned but t2 still null
    _t2 = ptr;
    return 1;
  } else {
    return -1;
  }
}

// get hashkey
std::string TPType::get_hashkey() {
  std::string tmp = "";
  if (_t1 == NULL && _t2 == NULL) {// not initialized
    if (type == Integer) {
      tmp += "Integer";
      return tmp;
    } else {
      return NULL;
    }
  } else if (_t2 == NULL) {// t2 is null => only one subtype
    switch(type) {
    case Sequence:
      tmp += "Sequence of ";
      tmp += _t1->get_hashkey();
      break;
    case Set:
      tmp += "Set of ";
      tmp += _t1->get_hashkey();
      break;
    default:
      break;
    }
  } else {
    switch(type) {
    case Relation:
      tmp += "Relation from ";
      tmp += _t1->get_hashkey();
      tmp += " to ";
      tmp += _t2->get_hashkey();
      break;
    case Function:
      tmp += "Function from ";
      tmp += _t1->get_hashkey();
      tmp += " to ";
      tmp += _t2->get_hashkey();
      break;
    default:
      break;
    }
  }
  return tmp;
}

// assign name
void TPType::assign_name(std::string _name) {
  type_name = _name;
}

// get type name
std::string TPType::get_name() {
  return type_name;
}

// get type id
int TPType::get_id() {
  return id;
}
